﻿/**
 *       Project: ServerLib
 *     Copyright: Copyright (C) 2008, Rob Kennedy
 *   Create Date: 2008-14-07 4:43 PM
 * Modified Date: 2008-14-07 4:43 PM    RK  Added comments
 *      Comments: Ignore SA1633.
 *      
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace ServerLib.Core
{
    /// --------------------------------------------------------------------
    /// Project	 : ServerLib
    /// Class	 : Paths
    /// Inherits : N/A
    /// --------------------------------------------------------------------
    /// <summary>
    ///  Contains useful static methods for quickly retrieving path 
    ///  information within a given path string and more.
    /// </summary>
    /// <authorname>Rob Kennedy</authorname>
    /// <remarks>
    /// </remarks>
    /// --------------------------------------------------------------------
    public class Paths
    {
        #region Public Properties
        /// <summary>
        /// Gets the full folder path of the currently executing assembly
        /// </summary>
        public static string AppPath
        {
            get
            {
                return AppendSlash(GetPath(System.Reflection.Assembly.GetExecutingAssembly().Location));
            }
        }
        #endregion

        #region Public Methods

        /// <summary>
        ///  Append a ending slash to a path value
        /// </summary>
        /// <param name="path">The path to append a slash to</param>
        /// <returns>the passed path with an appended \ backward slash</returns>
        public static string AppendSlash(string path)
        {
            return AppendSlash(path, "\\");
        }

        /// <summary>
        ///  Appends a specified slash to a path value
        /// </summary>
        /// <param name="path">The path to append a slash to</param>
        /// <param name="slashCharacter">The character to append to the path if it doesnt already exist</param>
        /// <returns>the passed path with an appended slashCharacter character</returns>
        public static string AppendSlash(string path, string slashCharacter)
        {
            string resultValue = slashCharacter;

            if (path != null ? path.Length > 0 : false)
            {
                if (path.LastIndexOf(slashCharacter) != path.Length - 1)
                {
                    resultValue = path + slashCharacter;
                }
                else
                {
                    resultValue = path;
                }
            }

            return resultValue;
        }

        /// <summary>
        ///  Strips a specified slash character from end of path
        /// </summary>
        /// <param name="path">The path to strip \ backward slash from</param>
        /// <returns>the passed path with ending backward slash removed</returns>
        public static string StripSlash(string path)
        {
            return StripSlash(path, "\\");
        }

        /// <summary>
        ///  Strips a specified slash character from end of path
        /// </summary>
        /// <param name="path">The path to strip a specified slash from</param>
        /// <param name="slashCharacter">The specified character to strip from end of path</param>
        /// <returns>The passed path with ending slashCharacter removed</returns>
        public static string StripSlash(string path, string slashCharacter)
        {
            string resultValue = path;
            if (path != null ? path.Length > 0 : false)
            {
                if (path.LastIndexOf(slashCharacter) == path.Length - 1)
                {
                    resultValue = path.Substring(0, path.Length - 1);
                }
            }

            return resultValue;
        }

        /// <summary>
        ///  returns the path/folder/url portion of a specified file path
        /// </summary>
        /// <param name="path">The document path to parse</param>
        /// <returns>Returns the folder path/url of the passed file path</returns>
        public static string GetPath(string path)
        {
            string resultValue = string.Empty;

            if (!Types.IsNullorEmpty(path))
            {
                int idx = path.LastIndexOf("\\");
                if (idx == -1)
                {
                    idx = path.LastIndexOf("/");
                }

                if (idx > -1)
                {
                    resultValue = path.Substring(0, idx);
                }
                else
                {
                    resultValue = path;
                }
            }

            return resultValue;
        }

        /// <summary>
        ///  returns the filename portion of a specified file path
        /// </summary>
        /// <param name="path">The document path to parse</param>
        /// <returns>Returns the file name of the passed file path</returns>
        public static string GetFileName(string path)
        {
            string resultValue = string.Empty;

            if (!Types.IsNullorEmpty(path))
            {
                int idx = path.LastIndexOf("\\");
                if (idx == -1)
                {
                    idx = path.LastIndexOf("/");
                }

                if (idx > -1)
                {
                    resultValue = path.Substring(idx + 1);
                }
                else
                {
                    resultValue = path;
                }
            }

            return resultValue;
        }

        #endregion
    }
}
